package com.geek.service.impl;

import com.geek.bean.PageData;
import com.geek.bean.data.ResponseBrandListData;
import com.geek.mapper.MarketBrandMapper;
import com.geek.model.MarketBrand;
import com.geek.model.MarketBrandExample;
import com.geek.service.BrandService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wbw
 * @data 2024/06/17 14:47
 **/

@Service
public class BrandServiceImpl implements BrandService{
    @Autowired
    MarketBrandMapper brandMapper;
    @Override
    public List<MarketBrand> list(int page, int limit) {
        PageHelper.startPage(page,limit);
        MarketBrandExample example = new MarketBrandExample();
        example.createCriteria().andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        return brandMapper.selectByExample(example);
    }

    @Override
    public MarketBrand findById(Integer brandId) {
        MarketBrand marketBrand = brandMapper.selectByPrimaryKey(brandId);
        return marketBrand;

    }

    @Override
    public PageData listForPage(Integer page, Integer limit) {
        List<MarketBrand> brandList = this.list(page, limit);
        List<ResponseBrandListData> list = new ArrayList<>();
        for (MarketBrand brand : brandList) {
            ResponseBrandListData build = ResponseBrandListData.builder()
                    .id(brand.getId())
                    .name(brand.getName())
                    .desc(brand.getDesc())
                    .picUrl(brand.getPicUrl())
                    .floorPrice(brand.getFloorPrice()).build();
            list.add(build);
        }
        PageData data = PageData.list(list);
        return data;
    }
}